import React, { Component } from 'react';
import CheckSearch from './checkSearch';
import CheckTable from './CheckTable';
import '../InsuranceOrder2/index.scss';
import api from '../../../api/api';
import { connectRouter } from '../../../mixins/router';
import _ from 'underscore';
class CheckOrderList extends Component {
  constructor(props) {
    super(props);
    this.state = {
      tableData: {},
      searchData: {},
      pageSize: 10,
      pageIndex: 1,
    };
  }
  componentWillMount() {
    this.searchDataFn({});
  }

  searchDataFn = async (data, submitClick) => {
    if (submitClick) {
      this.setState({
        searchData: data,
      });
    } else {
      data = this.state.searchData;
    }
    const pageIndex = submitClick ? 1 : this.state.pageIndex;
    const pageSize = this.state.pageSize;
    const skip = pageSize * (pageIndex - 1);
    const limit = pageSize;
    let tableData = {};

    // 去除查询空格
    Object.keys(data).forEach((item) => {
      if (data[item]) {
        data[item] = data[item].trim();
      }
    });

    tableData.list = await api.getPhysicalExaminationList(data, skip, limit + 1);


    if (tableData.list) {
      if (tableData.list.length > limit) {
        tableData.count = skip + limit + 1;
        tableData.list = tableData.list.slice(0, limit);
      } else {
        tableData.count = skip + (tableData.list ? tableData.list.length : 0);
      }
    } else {
      //兼容不存在list属性的数据结构
      tableData.count = skip + (tableData.length ? tableData.length : 0);
      tableData.list = Array.isArray(tableData) && tableData.slice(0, limit);
    }
    tableData.pageIndex = Number(submitClick ? 1 : this.state.pageIndex);
    tableData.pageSize = Number(this.state.pageSize);
    this.setState({
      tableData: tableData,
    });
  };
  pageChange = (pagination) => {
    const pageSize = pagination ? pagination.pageSize : this.state.pageSize;
    const pageIndex = pagination ? pagination.current : this.state.pageIndex;
    this.setState(
      {
        pageSize: pageSize,
        pageIndex: pageIndex,
      },
      () => {
        this.searchDataFn({});
      },
    );
  };
  render() {
    return (
      <div>
        <CheckSearch searchDataFn={this.searchDataFn}></CheckSearch>
        <CheckTable tableData={this.state.tableData} searchDataFn={this.searchDataFn} pageChange={this.pageChange}></CheckTable>
      </div>
    );
  }
}

export default connectRouter(CheckOrderList);
